package com.bzsg.rightsManagementSystem.util

import com.bzsg.rightsManagementSystem.util.result.RequestResult
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonParser


object GsonUtil {
    fun <T> getObjectList(jsonString: String?, cls: Class<T>?): List<T> {
        val list: MutableList<T> = ArrayList()
        try {
            val gson = Gson()
            val jsonBean = gson.fromJson<RequestResult<*>>(jsonString) //获取Result数据
            val array: JsonArray = JsonParser.parseString(jsonBean.data.toString()).asJsonArray //将Data注入实体类的list中
            for (jsonElement in array) {
                list.add(gson.fromJson(jsonElement, cls))
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return list
    }

}

inline fun <reified T : Any> Gson.fromJson(json: String?): T {
    return Gson().fromJson(json, T::class.java)
}

